<?php

	class ArticleAction extends BaseAction{
		
		public function index(){
			$articleModel = M("Article");
			$type=$this->_param("type");
			import('ORG.Util.Page'); // 导入分页类
			if($type){
				$where['type']=$type;
				$this->assign("type",$type);
				}
			$count = $articleModel->where($where)->count(); // 查询满足要求的总记录数 $map表示查询条件
			$Page = new Page($count, 10); // 实例化分页类 传入总记录数
			$show = $Page->show(); // 分页显示输出
			// 进行分页数据查询
			$list = $articleModel->where($where)->limit($Page->firstRow . ',' . $Page->listRows)->select();
			$this->assign('list', $list); // 赋值数据集
			$this->assign('page', $show); // 赋值分页输出
			$this->display();
			}
			
		public function add(){
			$this->display();
			}
		
		public function add_do(){
			$articleModel = M("Article");
			$desc=$this->_param("desc");
			$content=$_POST['content'];
			if($articleModel->create()) {
				$articleModel->content=$content;
				$articleModel->date=date("YmdHis");
				if(!$desc) $articleModel->desc=substr(strip_tags($content),0,210);
				$result =   $articleModel->add();
				if($result) {
					$this->success('操作成功！');
					}
				else{
					$this->error('写入错误！');
					}
				}
			else{
				$this->error($articleModel->getError());
				}
			}	
		
		public function edit(){
			$id=$this->_param(id);
			$articleModel = M("Article");
			$article=$articleModel->select($id);
			$this->assign("article",$article[0]);
			$this->display();			
			}
		
		public function edit_do(){
			$articleModel   =   D('Article');
			$content=$_POST['content'];
			if($articleModel->create()) {
				$articleModel->content=$content;
				$result =   $articleModel->save();
				if($result) {
					$this->success('操作成功！');
				}else{
					$this->error('写入错误！');
				}
			}else{
				$this->error($articleModel->getError());
			}
			}	
		
		public function del(){
			$id=$this->_param(id);
			$articleModel = M("Article");
			$article=$articleModel->delete($id);
			$this->redirect("/Article/index");
			}		
		
		}

?>